﻿Imports System.Linq
Imports System.Data
Imports System.Web
Imports Equipment.DataAccess
Imports System.Globalization

Partial Public Class WorkOrderMeasure


#Region "Properties"
    Private _db As EquipmentDataContext
    Public Property DBContext As EquipmentDataContext
        Get
            Return _db
        End Get
        Set(ByVal value As EquipmentDataContext)
            _db = value
        End Set
    End Property
#End Region

#Region "Public Section"


    Public Shared Function RetrieveAllByWorkOrderID(ByVal WorkOrderID As Integer) As IQueryable(Of WorkOrderMeasure)
        Return From m In Database.Instance.WorkOrderMeasures Where m.WorkOrderID = WorkOrderID
    End Function


    Public Shared Function GetByPrimaryKey(ByVal MeasureID As Integer) As WorkOrderMeasure
        Dim db As EquipmentDataContext = Database.Instance
        Dim result = From m In db.WorkOrderMeasures Where m.ID = MeasureID
        Dim measureEntity As WorkOrderMeasure = result.FirstOrDefault()
        measureEntity.DBContext = db
        Return measureEntity
    End Function

    Public Shared Sub Delete(ByVal measureToDelete As WorkOrderMeasure)

        Dim _dataBase As EquipmentDataContext = Database.Instance
        Dim measureQuery = From m In _dataBase.WorkOrderMeasures Where m.ID = measureToDelete.ID
        Dim deletingMeasure As WorkOrderMeasure = measureQuery.SingleOrDefault()

        _dataBase.WorkOrderMeasures.DeleteOnSubmit(deletingMeasure)
        Try
            _dataBase.SubmitChanges()

        Catch ex As Exception
            ' logger.Error(exception.Message + ":" + exception.StackTrace);
        End Try

    End Sub

    Public Sub SetValue(ByVal val As String)
        Dim measure As WorkOrderMeasure = WorkOrderMeasure.GetByPrimaryKey(Me.ID)
        If measure IsNot Nothing Then
            measure.CalibratedValue = val
            measure.Save()
        End If
    End Sub

    Public Sub SetComments(ByVal val As String)
        Dim measure As WorkOrderMeasure = WorkOrderMeasure.GetByPrimaryKey(Me.ID)
        If measure IsNot Nothing Then
            measure.MeasureComments = val
            measure.Save()
        End If
    End Sub

    Public Sub Save()
        If Me.DBContext IsNot Nothing Then
            Dim _dataBase As EquipmentDataContext = Me.DBContext

            Try
                _dataBase.SubmitChanges()
            Catch ex As Exception
                ' logger.Error(exception.Message + ":" + exception.StackTrace);
            End Try
        End If
    End Sub


    'Public Shared Sub Update(ByVal measureID As Integer, ByVal measureDescription As String, ByVal measureType As Integer, _
    '                        ByVal lookupValue As String, ByVal optimalLookupValue As String, _
    '                        ByVal integerMin As String, ByVal integerMax As String, _
    '                        ByVal decimalMin As String, ByVal decimalMax As String)

    '    Dim provider = New CultureInfo("en-US")
    '    Dim _dataBase As EquipmentDataContext = Database.Instance
    '    Dim measureQuery = From p In _dataBase.WorkOrderMeasures
    '    Dim measureToUpdate As Measure = measureQuery.SingleOrDefault(Function(m) m.MeasureID = measureID)

    '    If Not measureToUpdate Is Nothing Then

    '        measureToUpdate.MeasureDescription = measureDescription
    '        measureToUpdate.MeasureType = measureType
    '        measureToUpdate.LookupValue = lookupValue
    '        measureToUpdate.IntegerMin = If(String.IsNullOrEmpty(integerMin), New Nullable(Of Integer), CInt(integerMin))
    '        measureToUpdate.IntegerMax = If(String.IsNullOrEmpty(integerMax), New Nullable(Of Integer), CInt(integerMax))
    '        measureToUpdate.DecimalMin = If(String.IsNullOrEmpty(decimalMin), New Nullable(Of Decimal), Decimal.Parse(decimalMin, provider))
    '        measureToUpdate.DecimalMax = If(String.IsNullOrEmpty(decimalMax), New Nullable(Of Decimal), Decimal.Parse(decimalMax, provider))
    '        measureToUpdate.OptimalLookupValue = optimalLookupValue

    '    End If

    '    Try
    '        _dataBase.SubmitChanges()
    '    Catch ex As Exception
    '        ' logger.Error(exception.Message + ":" + exception.StackTrace);
    '    End Try

    'End Sub

    'Public Shared Sub Insert(ByVal AssetID As Integer, ByVal measureDescription As String, ByVal MeasureType As Integer, _
    '                        ByVal lookupValue As String, ByVal optimalLookupValue As String, _
    '                        ByVal integerMin As String, ByVal integerMax As String, _
    '                        ByVal decimalMin As String, ByVal decimalMax As String)

    '    Dim _dataBase As EquipmentDataContext = Database.Instance
    '    Dim newMeasure As New Measure
    '    Dim provider = New CultureInfo("en-US")

    '    newMeasure.AssetID = AssetID
    '    newMeasure.MeasureDescription = measureDescription
    '    newMeasure.MeasureType = MeasureType
    '    newMeasure.LookupValue = lookupValue
    '    newMeasure.OptimalLookupValue = optimalLookupValue
    '    newMeasure.IntegerMin = If(String.IsNullOrEmpty(integerMin), New Nullable(Of Integer), CInt(integerMin))
    '    newMeasure.IntegerMax = If(String.IsNullOrEmpty(integerMax), New Nullable(Of Integer), CInt(integerMax))
    '    newMeasure.DecimalMin = If(String.IsNullOrEmpty(decimalMin), New Nullable(Of Decimal), Decimal.Parse(decimalMin, provider))
    '    newMeasure.DecimalMax = If(String.IsNullOrEmpty(decimalMax), New Nullable(Of Decimal), Decimal.Parse(decimalMax, provider))

    '    _dataBase.Measures.InsertOnSubmit(newMeasure)
    '    Try
    '        _dataBase.SubmitChanges()

    '    Catch ex As Exception
    '        ' logger.Error(exception.Message + ":" + exception.StackTrace);
    '    End Try

    'End Sub

    Public Shared Function From(ByRef wOrder As WorkOrder, ByRef reqMeasure As RequirementMeasure) As WorkOrderMeasure

        Dim measure As New WorkOrderMeasure()

        measure.RequiredComments = reqMeasure.RequiredComments
        measure.DecimalMax = reqMeasure.DecimalMax
        measure.DecimalMin = reqMeasure.DecimalMin
        measure.IntegerMax = reqMeasure.IntegerMax
        measure.IntegerMin = reqMeasure.IntegerMin

        measure.LookupValue = reqMeasure.LookupValue
        measure.MeasureDescription = reqMeasure.MeasureDescription
        measure.MeasureType = reqMeasure.MeasureType
        measure.OptimalLookupValue = reqMeasure.OptimalLookupValue
        measure.WorkOrderID = wOrder.ID

        Return measure

    End Function
#End Region
End Class
